package com.deep.bilibili.controller;


import com.deep.bilibili.config.UploadConfig;
import com.deep.bilibili.dao.FileMapper;
import com.deep.bilibili.entity.File;
import com.deep.bilibili.ffmpeg.FfmpegUtil;
import com.deep.bilibili.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Controller
@RequestMapping("/admin/file")
public class FileController {
    private static Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private UploadConfig uploadConfig;

    @GetMapping("/list")
    public String index(@RequestParam(defaultValue = "") String name,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer pageSize, Model model) {
        //
        page = page < 1 ? 1 : page;

        Map<String, Object> params = new HashMap<>();
        params.put("offset", (page - 1) * pageSize);
        params.put("size", pageSize);
        if(StringUtils.isNotBlank(name) && !("null").equals(name)){
            params.put("name", name);
        }

        List<File> files = fileMapper.getPageList(params);
        long total = (fileMapper.getPageTotal(params));
        logger.info(" total = {}", total);

        long maxPage = new BigDecimal(total).divide(new BigDecimal(pageSize), 0, BigDecimal.ROUND_UP).longValue();
        maxPage = maxPage > 0 ? maxPage : 1;
        logger.info(" maxPage = {}", maxPage);

        long nextPage = page < maxPage ? page + 1 : maxPage;
        long prevPage = page > 1 ? page - 1 : (page <= 0 ? 1 : page);
        logger.info(" nextPage = {}", nextPage);
        logger.info(" prevPage = {}", prevPage);

        model.addAttribute("total", total);
        model.addAttribute("files", files);
        if(StringUtils.isNotBlank(name) && !("null").equals(name)){
            model.addAttribute("name", name);
        }
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("page", page);
        model.addAttribute("nextPage", nextPage);
        model.addAttribute("prevPage", prevPage);
        model.addAttribute("maxPage", maxPage);

        return "file/index";

    }

    @GetMapping("/create")
    public String create() {

        return "file/create";
    }


    @PostMapping("/dodelete")
    @ResponseBody
    public Object dodelete(File file) {
        if(null == file.getId()){
            return ResponseUtil.fail(0,"id不能为空");
        }

        fileMapper.deleteByPrimaryKey(file.getId());

        return ResponseUtil.ok();
    }

    @PostMapping("/push")
    @ResponseBody
    public Object startPush(Integer fileId, String type) {
        logger.info(" fileId  {}", fileId);
        logger.info(" type  {}", type);

        File file = fileMapper.selectByPrimaryKey(fileId);
        //
        String uploadPath = uploadConfig.getUploadDir();

        String rtmpUrl = file.getRtmpUrl();
        String streamName = file.getStreamName();
        String fullPath = uploadPath + "/" + file.getPath();

        logger.info(" rtmpurl  {}", rtmpUrl);
        logger.info(" streamName  {}", streamName);
        logger.info(" fullPath  {}", fullPath);

        try {
            FfmpegUtil.rtmp(fullPath, streamName);
        } catch (Exception e) {
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok();

    }

}
